#include "registwidget.h"
#include "ui_registwidget.h"
#include "proto.h"


registwidget::registwidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::registwidget)
{
    ui->setupUi(this);
    //调整窗口大小
    this->resize(500,400);
    this->setMaximumSize(500,400);
    this->setMinimumSize(500,400);

    //窗口属性
    this->setWindowTitle(QString("账户注册"));
    QIcon windowIcon = QIcon(QPixmap(QString(":/new/prefix1/pic/QQ.jpg")));
    this->setWindowIcon(windowIcon);

    //背景图片
    QPalette pal;
    pal.setBrush(QPalette::Background,\
                 QBrush(QPixmap(QString(":/new/prefix1/pic/back.jpeg")).scaled(500,450)));
    this->setPalette(pal);

    //字体
    QFont font;
    font.setPixelSize(15);
    font.setBold(true);



    //注册用户名
    nameLabel = new QLabel(QString("用户名"));
    nameLabel->setFont(font);
   // nameLabel->setPalette()
    nameLineEdit = new QLineEdit();
    nameLineEdit->setPlaceholderText(QString("账号"));
    nameLineEdit->setFont(font);
    //布局用户名
    nameLayout = new QHBoxLayout();
    nameLayout->addWidget(nameLabel);
    nameLayout->addWidget(nameLineEdit);
    //注册密码
    passwordLabel = new QLabel(QString("密码"));
    passwordLabel->setFont(font);
    passwordLineEdit = new QLineEdit();
    passwordLineEdit->setPlaceholderText(QString("密码"));
    passwordLineEdit->setFont(font);
    passwordLineEdit->setEchoMode(QLineEdit::Password);
    //布局密码
    passwordLayout = new QHBoxLayout();
    passwordLayout->addWidget(passwordLabel);
    passwordLayout->addWidget(passwordLineEdit);
    //注册按钮
    registBtn = new QPushButton(QString("注册账户"));
    registBtn->setFont(font);




    //主布局
    mainLayout = new QVBoxLayout();
    mainLayout->setContentsMargins(80,20,80,50);
    mainLayout->addLayout(nameLayout);
    mainLayout->addLayout(passwordLayout);
    mainLayout->addWidget(registBtn);

    this->setLayout(mainLayout);

    //报时套接字
    sd = new QUdpSocket();

    //连接信号与曹
    connect(registBtn,SIGNAL(clicked(bool)),this,SLOT(on_clicket_regisbtn()));
    connect(sd,SIGNAL(readyRead()),this,SLOT(on_readyread_udp()));
}

registwidget::~registwidget()
{
    delete ui;
}

void registwidget::on_clicket_regisbtn()
{
    rgs_t sndbuf;

    QString cntstr = nameLineEdit->text();//获取选框的内容
    QString pwdstr = passwordLineEdit->text();

    strncpy(sndbuf.cntbuf,cntstr.toLatin1().data(),CNTSIZE);//将获取到的用户名复制到协议中
    QByteArray pwdbta = QCryptographicHash::hash(pwdstr.toLatin1(),QCryptographicHash::Sha512);//将口令原文加密
    strncpy(sndbuf.pwdbuf,pwdbta.toHex().data(),PWDSIZE);//将加密后的口令原文复制到协议中

    qDebug() << sndbuf.cntbuf <<sndbuf.pwdbuf ;

    sd->writeDatagram((char *)&sndbuf,sizeof(sndbuf),QHostAddress(RGS_IP),RGS_PORT);//打包数据包
}

void registwidget::on_readyread_udp()
{
    rgs_t rcvbuf;
    int ret;

    sd->readDatagram((char *)&rcvbuf,sizeof(rcvbuf));
    switch(rcvbuf.flag)
    {
    case REG_OK:
        QMessageBox::information(this,QString("注册请求"),QString("恭喜你，注册成功！"),QMessageBox::Ok);
        this->close();
        break;
    case REG_EXISTS:
        ret = QMessageBox::question(this,QString("注册请求"),QString("此账号已被注册，是否重新注册"),QMessageBox::Ok,QMessageBox::No);
        if(ret == QMessageBox::No)
            this->close();
        else
        {
            nameLineEdit->clear();
            passwordLineEdit->clear();
        }
        break;
    case REG_ERROR:
        break;
    default:
        break;
    }

}



